package servlet;

import dao.DepartmentDAOImpl;
import pojo.Department;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/department")
public class DepartmentServlet extends BaseServlet {

    // 添加部门
    protected void addDepartment(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        int departmentID = Integer.parseInt(req.getParameter("departmentID"));
        String departmentName = req.getParameter("departmentName");
        int companyID = Integer.parseInt(req.getParameter("companyID"));

        Department department = new Department(null, departmentName, companyID);
        DepartmentDAOImpl.add(department);
        sendJsonResponse(resp, new ApiResult(200, "Department added successfully.", null));
    }

    // 删除部门
    protected void deleteDepartment(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int departmentID = Integer.parseInt(req.getParameter("departmentID"));
        DepartmentDAOImpl.delete(departmentID);
        sendJsonResponse(resp, new ApiResult(200, "Department deleted successfully.", null));
    }

    // 更新部门
    protected void updateDepartment(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int departmentID = Integer.parseInt(req.getParameter("departmentID"));
        String departmentName = req.getParameter("departmentName");
        int companyID = Integer.parseInt(req.getParameter("companyID"));

        Department department = new Department(departmentID, departmentName, companyID);
        DepartmentDAOImpl.update(department);
        sendJsonResponse(resp, new ApiResult(200, "Department updated successfully.", null));
    }

    // 获取所有部门
    protected void getAllDepartments(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Department> departments = DepartmentDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", departments));
    }

    // 根据部门编号获取部门
    protected void getDepartmentById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int departmentID = Integer.parseInt(req.getParameter("departmentID"));
        Department department = DepartmentDAOImpl.getById(departmentID);
        sendJsonResponse(resp, new ApiResult(200, "Success", department));
    }

    // 获取部门数量
    protected void getDepartmentCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = DepartmentDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取部门
    protected void getDepartmentsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Department> departments = DepartmentDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", departments));
    }

    // 根据关键字搜索部门
    protected void findDepartmentsByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Department> departments = DepartmentDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", departments));
    }

    // 根据关键字分页搜索部门
    protected void findDepartmentsByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Department> departments = DepartmentDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", departments));
    }
}
